package _5thread;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-06-07 11:25
 */
public class ThreadLocalDemo {

    public static int age = 0;


    public static void main(String[] args) {

        test2();

    }


    public static void test2() {
        ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

        for (int i = 0; i < 5; i++) {

            new Thread(() -> {

                try {
                    Integer integer = threadLocal.get();
                    threadLocal.set(integer += 1);
                    System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());

                } finally {
                    threadLocal.remove();
                }

            }).start();


        }

    }
}
